<?php 
require('survey/Survey.php');
require('survey/SurveyDemography.php');
require('survey/SurveyQuestion.php');
require('survey/SurveyResponse.php');
require('survey/SurveySection.php');
class SurveyCore extends ObjectController
{
	public $SiteId;
	public $Survey;
	public $Section;
	public $Surveys;
	public $SubLoaded;
	public $Forms;
	
	public function SurveyCore()
	{
		global $katana;
		self::setSiteId($katana->getId());
		self::setSubLoaded(false);
		if($katana->getSurveyID() > 0)
		{ self::loadSurvey(); }
	}
	
	public function getSiteId() { return $this->SiteId; }
	public function getSurvey() { return $this->Survey; }
	public function getSurveys() { return $this->Surveys; }
	public function getSubLoaded() { return $this->SubLoaded; }
	public function getForms() { return $this->Forms; }
	
	public function setSiteId($val) { $this->SiteId=$val; }
	public function setSurvey($val) { $this->Survey=$val; }
	public function setSurveys($val) { $this->Surveys=$val; }
	public function setSubLoaded($val) { $this->SubLoaded=$val; }
	public function setForms($val) { $this->Forms=$val; }
	
	public function loadSurvey()
	{
		global $katana;
		global $com;
		
		if($katana->getId() > 0)
		{
			$sql = 'SELECT id, site_id, title, password, active, description, guide FROM '.DB_PERFIX.'survey WHERE id='.$katana->getSurveyID().' AND active=1;';
			$result = mysql_query($sql);
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{
					$this->Survey = new Survey();
					$this->Survey->setId($row['id']);
					$this->Survey->setSiteId($row['site_id']);
					$this->Survey->setTitle($row['title']);
					$this->Survey->setPassword($row['password']);
					$this->Survey->setActive($row['active']);
					$this->Survey->setDescription($row['description']);
					$this->Survey->setGuide($row['guide']);
					$this->Survey->loadSections();
					$this->Survey->loadDemogs();
					
					$katana->setTitle($this->Survey->Title);
					$katana->setComID(17);
					$com->setComId(17);
					$com->loadComponent($katana);
					self::loadForms();
				}
			}
		}
	}
	
	public function loadSurveys()
	{
		global $katana;
		if(($katana->getId() > 0) && ($this->SubLoaded == false))
		{
			$sql = 'SELECT id, site_id, title, active FROM '.DB_PERFIX.'survey WHERE site_id='.$katana->getId().' AND active=1 ORDER BY id ASC;';
			$result = mysql_query($sql);
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{
					$this->Surveys[$row['id']] = new Survey();
					$this->Surveys[$row['id']]->setId($row['id']);
					$this->Surveys[$row['id']]->setSiteId($row['site_id']);
					$this->Surveys[$row['id']]->setTitle($row['title']);
					$this->Surveys[$row['id']]->setActive($row['active']);
					$this->Surveys[$row['id']]->loadSections();
				}
			}
			$this->SubLoaded = true;
		}
	}

	public function loadForms()
	{
		if($this->Survey->Demogs)
		{
			$this->Forms['demog'] = new Form;
			$this->Forms['demog']->HumanCheck = 0;
			foreach($this->Survey->Demogs as $demog)
			{ 
				if($demog->Values)
				{
					$this->Forms['demog']->Fields['demog_'.$demog->Id] 	= new FormField('demog_'.$demog->Id, 'select', $demog->Title, true, true, false, 'num', 0, 500);
					foreach($demog->Values as $demval)
					{
						$this->Forms['demog']->Fields['demog_'.$demog->Id]->addOption($demval->Id, $demval->Value, ''); 
					}
				}
			}
			if($this->Survey->Password)
			{ 
				$this->Forms['demog']->Fields['password'] 	= new FormField('password', 'hidden', '', true, false, false, 'anum', 0, 500); 
				$this->Forms['demog']->Fields['password']->setValue($this->Survey->Password);
			}
		}
		$this->Forms['guide'] = new Form;
		$this->Forms['guide']->HumanCheck = 0;
		if($this->Survey->Password)
		{ 
			$this->Forms['guide']->Fields['password'] 	= new FormField('password', 'hidden', '', true, false, false, 'anum', 0, 500); 
			$this->Forms['guide']->Fields['password']->setValue($this->Survey->Password);
		}
		if($this->Survey->Demogs)
		{
			foreach($this->Survey->Demogs as $demog)
			{ 
				if($demog->Values)
				{
					$this->Forms['guide']->Fields['demog_'.$demog->Id] 	= new FormField('demog_'.$demog->Id, 'hidden', '', true, false, false, 'num', 0, 500); 
					$this->Forms['guide']->Fields['demog_'.$demog->Id]->setValue($_POST['demog_'.$demog->Id]);
				}
			}
		}
		if($this->Survey->Sections)
		{
			$this->Forms['sections'] = new Form;
			$this->Forms['sections']->HumanCheck = 0;
			if($this->Survey->Password)
			{ 
				$this->Forms['sections']->Fields['password'] 	= new FormField('password', 'hidden', '', true, false, false, 'anum', 0, 500); 
				$this->Forms['sections']->Fields['password']->setValue($this->Survey->Password);
			}
			if($this->Survey->Demogs)
			{
				foreach($this->Survey->Demogs as $demog)
				{ 
					if($demog->Values)
					{
						$this->Forms['sections']->Fields['demog_'.$demog->Id] 	= new FormField('demog_'.$demog->Id, 'hidden', '', true, false, false, 'num', 0, 500); 
						$this->Forms['sections']->Fields['demog_'.$demog->Id]->setValue($_POST['demog_'.$demog->Id]);
					}
				}
			}
		}
	}
}

?>